package com.ellecity06.cwhiskymall.ui.mine.auth

import android.app.Activity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.ktx.hasReadImagePermission
import com.ellecity06.common.ktx.load
import com.ellecity06.common.ktx.observeEvent
import com.ellecity06.common.ktx.upPath
import com.ellecity06.common.utils.ToastUtils
import com.ellecity06.common.utils.chooseImage
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.databinding.ActivityAuthNameBinding

/**
 * @author ellecity06
 * @time 2024/12/3 18:11
 * @des
 */
class AuthNameActivity : BaseMvvmActivity<ActivityAuthNameBinding, AuthNameViewModel>(), View.OnClickListener {
    override fun inflateBinding(): ActivityAuthNameBinding {
        return ActivityAuthNameBinding.inflate(layoutInflater)
    }

    override fun initialize(savedInstanceState: Bundle?) {
        title = "上传身份信息"


    }

    override fun initEvent() {
        binding.ivImageZhengmian.setOnClickListener(this)
        binding.ivImageFanmian.setOnClickListener(this)
        binding.tvConfirm.setOnClickListener(this)
    }

    override fun initObserver() {
        viewModel.state.observeEvent(this) {
            when (it) {
                is AuthNameState.UpdateFail -> {
                    if (it.isFace) {
                        binding.tvAuthName.text = ""
                        binding.tvAuthNumber.text = ""
                        binding.ivImageFanmian.setImageResource(R.mipmap.bg_image_auth_1)
                    } else {
                        binding.ivImageFanmian.setImageResource(R.mipmap.bg_image_auth_2)
                    }

                    ToastUtils.show("识别失败，请重新选择")
                }

                is AuthNameState.UpdateSuccess -> {
                    binding.tvAuthName.text = it.data.name
                    binding.tvAuthNumber.text = it.data.idNumber
                }

                AuthNameState.SubmitSuccess -> {
                    ToastUtils.show("认证成功")
                    setResult(Activity.RESULT_OK)
                    finish()
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            binding.ivImageZhengmian.id -> {
                chooseImage(true)
            }

            binding.ivImageFanmian.id -> {
                chooseImage(false)
            }

            binding.tvConfirm.id -> {
                lifecycleScope.launchWhenCreated {
                    val name = binding.tvAuthName.text.trim().toString()
                    val number = binding.tvAuthNumber.text.trim().toString()
                    viewModel.intentChannel.send(AuthNameIntent.Submit(number, name))
                }
            }
        }
    }

    private fun chooseImage(isFont: Boolean = true) {
        hasReadImagePermission(content = "获取相片需要您的存储权限，当您需要拍照是会需要您的相机权限拍摄相片") {
            chooseImage(compress = true) {
                val localMedia = it[0]
                val upPath = localMedia?.upPath()
                if (upPath != null) {
                    lifecycleScope.launchWhenCreated {
                        viewModel.intentChannel.send(AuthNameIntent.UploadImage(upPath, isFont))
                    }
                    if (isFont) {
                        binding.ivImageZhengmian.load(this, upPath)
                    } else {
                        binding.ivImageFanmian.load(this, upPath)
                    }
                } else {
                    ToastUtils.show("请重新选择")
                }


            }
        }
    }
}